package cn.mugutu.webrtc.websocket.interceptor;

import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.http.server.ServerHttpRequest;
import org.springframework.http.server.ServerHttpResponse;
import org.springframework.http.server.ServletServerHttpRequest;
import org.springframework.web.socket.WebSocketHandler;
import org.springframework.web.socket.server.support.HttpSessionHandshakeInterceptor;

//拦截器,只是在初次建立连接的时候执行一次,在到达onOpen之前执行,可以通过httpRequest获取
public class HandshakeInterceptor extends HttpSessionHandshakeInterceptor{
	
	
	 //初次握手访问前
	public boolean beforeHandshake(ServerHttpRequest request,ServerHttpResponse response, WebSocketHandler wsHandler,Map<String, Object> attributes) throws Exception {
		
		if (request instanceof ServletServerHttpRequest) {
            HttpServletRequest servletRequest = ((ServletServerHttpRequest) request).getServletRequest();
            String type = servletRequest.getParameter("type");
            String uid = servletRequest.getParameter("uid");
            String toId = servletRequest.getParameter("toId");
            
            attributes.put("type", type);
            attributes.put("uid", uid);
            attributes.put("toId", toId);
        }
		
        return true;
	}

	
	//初次握手访问后
	public void afterHandshake(ServerHttpRequest request,ServerHttpResponse response, WebSocketHandler wsHandler,Exception exception) {
		
	}

}
